/* Includes ------------------------------------------------------------------*/
#include "page_main.h"
#include "lvgl/lvgl.h"
#include "main.h"
#include "stdlib.h"
#include "stdio.h"
#include "puresun_func.h"
#include "toast.h"
#include "icons.h"
#include "msg_box.h"
#include "jsmn_parser.h"
#include "puresun_func.h"
#include "lock.h"
#include "485_slave.h"
#include "math.h"
/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
void reset_box_btn_style(left_box_t *box);
void draw_device_page(main_box_t *box);

void btn_close_all_device_cb(lv_event_t *event);

void mpump_ctl_cb(lv_event_t *event);
void btn_high_rs_event_cb(lv_event_t *event);
void btn_low_rs_event_cb(lv_event_t *event);

void relay_jpump_ctl_cb(lv_event_t *event);
void relay_mpump_ctl_cb(lv_event_t *event);
void relay_vlave_main_ctl_cb(lv_event_t *event);

void jpump_ctl_cb(lv_event_t *event);
void mpump_switch_cb(lv_event_t *event);
void btn_jpump_reset_event_cb(lv_event_t *event);
void valve_main_ctl_cb(lv_event_t *event);
// void valve_release_ctl_cb(lv_event_t *event);
void device_ctl_cb(lv_event_t *event);

// void btn_output_log_cb(lv_event_t *event);
lv_align_t layout_convert_align(string align);
void layout_scr(lv_obj_t *parent, string page, string theme);

lv_obj_t *draw_info_label_format(lv_obj_t **label_value, string name, lv_obj_t *parent);
lv_obj_t *draw_btn_radio_format(lv_obj_t **btn, string name, int size, lv_obj_t *parent, lv_event_cb_t cb);
// lv_obj_t *draw_btn(string btn_lable, lv_event_cb_t cb, lv_color_t bg_color, lv_obj_t *parent, void *user_data);
void draw_main_ctl_box(lv_obj_t *main_ctl_box);
void hook_match(string hook, lv_obj_t *obj);
void clean_page(void);
void btn_unlock_cb(lv_event_t *event);
void unlock_cb(void *args);
void btn_lock_cb(lv_event_t *event);
void operate_mpump_close_cb(void);
void operate_mpump_open_cb(void);

/* Private functions ---------------------------------------------------------*/

void page_main(void)
{
    ui_t *ui = &device.ui;
    device.current_page = PAGE_DEVICE;
    lv_obj_clean(ui->scr);

    // create keyboard
    ui->keyboard = lv_keyboard_create(lv_scr_act());
    lv_obj_set_style_text_font(ui->keyboard, device.ui.fnt.size_24.font, 0);
    lv_obj_set_size(ui->keyboard, LV_HOR_RES_MAX, LV_VER_RES_MAX / 2);
    lv_obj_add_flag(ui->keyboard, LV_OBJ_FLAG_HIDDEN);

    // lobj[LID_BAR_TIME] = &ui->status_bar;
    status_bar_t *status_bar = &ui->status_bar;
    status_bar_create(status_bar, ui->scr);
    // status_bar_set_icon(status_bar, "wifi", ICON_WIFI_NO_SIGNAL);
    status_bar_set_icon(status_bar, "wifi2", ICON_WIFI_NO_INTERNET);

    ui->main_box.box = lv_obj_create(device.ui.scr);
    lv_obj_set_size(ui->main_box.box, LV_HOR_RES_MAX, LV_VER_RES_MAX - 30);
    lv_obj_set_pos(ui->main_box.box, 0, 30);

    draw_device_page(&ui->main_box);
    lv_disp_load_scr(ui->scr);
}

/******************************************************************************
 * Function Name  : layout_convert_align
 * Description    : convert string to align type
 * Input          : [string align]
 *                  align: the string of align
 * Output         : None
 * Return         : [lv_align_t align]
 *                  align: the align type
 *******************************************************************************/
lv_align_t layout_convert_align(string align)
{
    if (strcmp(align, "auto") == 0)
        return LV_ALIGN_DEFAULT;
    if (strcmp(align, "top_left") == 0)
        return LV_ALIGN_TOP_LEFT;
    if (strcmp(align, "top_mid") == 0)
        return LV_ALIGN_TOP_MID;
    if (strcmp(align, "top_right") == 0)
        return LV_ALIGN_TOP_RIGHT;
    if (strcmp(align, "bottom_left") == 0)
        return LV_ALIGN_BOTTOM_LEFT;
    if (strcmp(align, "bottom_mid") == 0)
        return LV_ALIGN_BOTTOM_MID;
    if (strcmp(align, "bottom_right") == 0)
        return LV_ALIGN_BOTTOM_RIGHT;
    if (strcmp(align, "left_mid") == 0)
        return LV_ALIGN_LEFT_MID;
    if (strcmp(align, "right_mid") == 0)
        return LV_ALIGN_RIGHT_MID;
    if (strcmp(align, "center") == 0)
        return LV_ALIGN_CENTER;

    if (strcmp(align, "out_top_left") == 0)
        return LV_ALIGN_OUT_TOP_LEFT;
    if (strcmp(align, "out_top_mid") == 0)
        return LV_ALIGN_OUT_TOP_MID;
    if (strcmp(align, "out_top_right") == 0)
        return LV_ALIGN_OUT_TOP_RIGHT;
    if (strcmp(align, "out_bottom_left") == 0)
        return LV_ALIGN_OUT_BOTTOM_LEFT;
    if (strcmp(align, "out_bottom_mid") == 0)
        return LV_ALIGN_OUT_BOTTOM_MID;
    if (strcmp(align, "out_bottom_right") == 0)
        return LV_ALIGN_OUT_BOTTOM_RIGHT;
    if (strcmp(align, "out_left_top") == 0)
        return LV_ALIGN_OUT_LEFT_TOP;
    if (strcmp(align, "out_left_mid") == 0)
        return LV_ALIGN_OUT_LEFT_MID;
    if (strcmp(align, "out_left_bottom") == 0)
        return LV_ALIGN_OUT_LEFT_BOTTOM;
    if (strcmp(align, "out_right_top") == 0)
        return LV_ALIGN_OUT_RIGHT_TOP;
    if (strcmp(align, "out_right_mid") == 0)
        return LV_ALIGN_OUT_RIGHT_MID;
    if (strcmp(align, "out_right_bottom") == 0)
        return LV_ALIGN_OUT_RIGHT_BOTTOM;
    return LV_ALIGN_DEFAULT;
}
// box->page_device.mpump_rotational_speed = lv_label_create(box->box);
// lv_obj_set_style_text_font(box->page_device.mpump_rotational_speed, device.ui.fnt.size_36.font, 0);
// lv_label_set_text(box->page_device.mpump_rotational_speed, "转速:");
// lv_obj_align_to(box->page_device.mpump_rotational_speed, box->page_device.mpump_info_box, LV_ALIGN_TOP_MID, 0, 0);
// lobj[LID_DEV_PMP_RS] = lv_label_create(box->box);
// lv_obj_set_style_text_font(lobj[LID_DEV_PMP_RS], device.ui.fnt.size_20.font, 0);
// lv_label_set_text(lobj[LID_DEV_PMP_RS], "0 Hz");
// lv_obj_align_to(lobj[LID_DEV_PMP_RS], box->page_device.mpump_rotational_speed, LV_ALIGN_OUT_RIGHT_MID, 30, 0);
lv_obj_t *draw_info_label_format(lv_obj_t **label_value, string name, lv_obj_t *parent)
{
    lv_obj_t *label_name = lv_label_create(parent);
    *label_value = lv_label_create(parent);
    lv_label_set_text(label_name, name);
    lv_obj_set_style_text_font(label_name, device.ui.fnt.size_24.font, 0);
    lv_label_set_text(*label_value, "0.0");
    lv_obj_set_style_text_font(*label_value, device.ui.fnt.size_24.font, 0);
    // lv_obj_align_to(*label_value, parent, LV_ALIGN_CENTER, 10, 0);
    // lv_obj_align_to(*label_value, label_name, LV_ALIGN_OUT_RIGHT_TOP, 0, 0);
    return label_name;
}
lv_obj_t *draw_btn_radio_format(lv_obj_t **btn, string name, int size, lv_obj_t *parent, lv_event_cb_t cb) // size 20 35
{
    lv_obj_t *box = lv_obj_create(parent);
    lv_obj_t *label = lv_label_create(box);
    *btn = lv_btn_create(box);

    lv_obj_set_size(box, size + 140, size + 10);
    lv_obj_set_style_radius(box, size, 0);
    lv_obj_set_style_bg_color(box, lv_color_hex(0x2F2F3F), 0);
    lv_obj_clear_flag(box, LV_OBJ_FLAG_SCROLLABLE);

    lv_label_set_text(label, name);
    if (size == 20)
        lv_obj_set_style_text_font(label, device.ui.fnt.size_16.font, 0);
    else if (size == 35)
        lv_obj_set_style_text_font(label, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(label, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(label, box, LV_ALIGN_LEFT_MID, 0, 0);

    lv_obj_set_size(*btn, size, size);
    lv_obj_set_style_radius(*btn, size, 0);
    lv_obj_set_style_bg_color(*btn, lv_color_hex(0x9E9E9E), 0);
    lv_obj_align_to(*btn, box, LV_ALIGN_RIGHT_MID, 17, 0);
    lv_obj_add_event_cb(*btn, cb, LV_EVENT_CLICKED, NULL);

    return box;
}

void clean_page(void)
{
    // page main
    lobj[LID_PM_GLOMMY] = NULL;
    lobj[LID_PM_LOG_LABLE] = NULL;
    lobj[LID_PM_VACUUM_LABLE] = NULL;
    lobj[LID_PM_BTN_START] = NULL;
    lobj[LID_PM_POWER_VALUE_LABEL] = NULL;
    lobj[LID_PM_RUNTIME] = NULL;
}

void draw_device_page(main_box_t *box)
{
    lv_obj_clean(box->box);
    lv_obj_clear_flag(box->box, LV_OBJ_FLAG_SCROLLABLE);
    /*device status box start*/
    box->page_device.device_ctl_box = lv_obj_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.device_ctl_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.device_ctl_box, 0, 0);
    lv_obj_set_size(box->page_device.device_ctl_box, 205, 400);
    lv_obj_align_to(box->page_device.device_ctl_box, box->box, LV_ALIGN_BOTTOM_LEFT, 100, 0);
    lv_obj_clear_flag(box->page_device.device_ctl_box, LV_OBJ_FLAG_SCROLLABLE);

    box->page_device.device_ctl_title = lv_label_create(box->box);
    lv_label_set_text(box->page_device.device_ctl_title, "设备控制");
    lv_obj_set_style_text_font(box->page_device.device_ctl_title, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.device_ctl_title, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_pad_all(box->page_device.device_ctl_title, 0, 0);
    lv_obj_align_to(box->page_device.device_ctl_title, box->page_device.device_ctl_box, LV_ALIGN_TOP_MID, 0, 0);

    /*--------------------------------------------------------------------------------------*/
    box->page_device.jpump_ctl_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_JPUMP], "机械泵", 35, box->page_device.device_ctl_box, jpump_ctl_cb);
    box->page_device.vavle_main_status_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_VALVE_MAIN], "主阀", 35, box->page_device.device_ctl_box, valve_main_ctl_cb);
    box->page_device.mpump_ctl_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPUMP], "分子泵", 35, box->page_device.device_ctl_box, mpump_switch_cb);
    box->page_device.vavle_release_status_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_VALVE_RELEASE], "放气阀", 35, box->page_device.device_ctl_box, NULL);
    lv_obj_align_to(box->page_device.jpump_ctl_box, box->page_device.device_ctl_title, LV_ALIGN_OUT_BOTTOM_LEFT, -40, 25);
    lv_obj_align_to(box->page_device.vavle_main_status_box, box->page_device.jpump_ctl_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    lv_obj_align_to(box->page_device.mpump_ctl_box, box->page_device.vavle_main_status_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    lv_obj_align_to(box->page_device.vavle_release_status_box, box->page_device.mpump_ctl_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);

    /*--------------------------------------------------------------------------------------*/
    lobj[LID_DEV_BTN_DEVICE_CTL] = lv_btn_create(box->box);
    lv_obj_set_style_bg_color(lobj[LID_DEV_BTN_DEVICE_CTL], lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_style_radius(lobj[LID_DEV_BTN_DEVICE_CTL], 5, 0);
    lv_obj_set_size(lobj[LID_DEV_BTN_DEVICE_CTL], 180, 50);
    lv_obj_align_to(lobj[LID_DEV_BTN_DEVICE_CTL], box->page_device.vavle_release_status_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);
    lv_obj_add_event_cb(lobj[LID_DEV_BTN_DEVICE_CTL], device_ctl_cb, LV_EVENT_CLICKED, NULL);

    box->page_device.device_ctl_btn_label = lv_label_create(lobj[LID_DEV_BTN_DEVICE_CTL]);
    device.autorun.start = false;
    if (device.autorun.start)
        lv_label_set_text(box->page_device.device_ctl_btn_label, "停止");
    else
        lv_label_set_text(box->page_device.device_ctl_btn_label, "启动");
    lv_obj_set_style_text_font(box->page_device.device_ctl_btn_label, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.device_ctl_btn_label, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(box->page_device.device_ctl_btn_label, box->page_device.device_ctl_btn, LV_ALIGN_CENTER, 0, 0);
    /*--------------------------------------------------------------------------------------*/
    /*device status box end*/
    /*mpump box start*/
    box->page_device.mpump_box = lv_obj_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.mpump_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.mpump_box, 0, 0);
    lv_obj_set_size(box->page_device.mpump_box, 370, 400);
    lv_obj_clear_flag(box->page_device.mpump_box, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_align_to(box->page_device.mpump_box, box->page_device.device_ctl_box, LV_ALIGN_OUT_RIGHT_BOTTOM, 5, 0);

    box->page_device.mpump_title = lv_label_create(box->box);
    lv_label_set_text(box->page_device.mpump_title, "分子泵 FF-300");
    lv_obj_set_style_text_font(box->page_device.mpump_title, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.mpump_title, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_pad_all(box->page_device.mpump_title, 0, 0);
    lv_obj_align_to(box->page_device.mpump_title, box->page_device.mpump_box, LV_ALIGN_TOP_MID, 0, -5);

    box->page_device.mpump_info_box = lv_obj_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.mpump_info_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.mpump_info_box, 0, 0);
    lv_obj_set_size(box->page_device.mpump_info_box, 350, 165);
    lv_obj_align_to(box->page_device.mpump_info_box, box->page_device.mpump_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 3);
    lv_obj_clear_flag(box->page_device.mpump_info_box, LV_OBJ_FLAG_SCROLLABLE);

    box->page_device.mpump_voltage = draw_info_label_format(&lobj[LID_DEV_MPM_VOLTAGE], "电压:", box->page_device.mpump_info_box);
    box->page_device.mpump_current = draw_info_label_format(&lobj[LID_DEV_MPM_CURRENT], "电流:", box->page_device.mpump_info_box);
    box->page_device.mpump_rotational_speed = draw_info_label_format(&lobj[LID_DEV_MPM_RS], "转速:", box->page_device.mpump_info_box);
    lv_obj_set_style_text_font(lobj[LID_DEV_MPM_RS], device.ui.fnt.size_20.font, 0);
    box->page_device.mpump_temperature = draw_info_label_format(&lobj[LID_DEV_MPM_TEMPERATURE], "温度:", box->page_device.mpump_info_box);
    lv_obj_align_to(box->page_device.mpump_voltage, box->page_device.mpump_info_box, LV_ALIGN_TOP_MID, 30, -5);
    lv_obj_align_to(lobj[LID_DEV_MPM_VOLTAGE], box->page_device.mpump_voltage, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(box->page_device.mpump_current, box->page_device.mpump_voltage, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_MPM_CURRENT], box->page_device.mpump_current, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(box->page_device.mpump_rotational_speed, box->page_device.mpump_current, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_MPM_RS], box->page_device.mpump_rotational_speed, LV_ALIGN_OUT_RIGHT_TOP, 10, 2);
    lv_obj_align_to(box->page_device.mpump_temperature, box->page_device.mpump_rotational_speed, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_MPM_TEMPERATURE], box->page_device.mpump_temperature, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);

    box->page_device.btn_low_rs = lv_btn_create(box->page_device.mpump_info_box);
    lv_obj_set_style_radius(box->page_device.btn_low_rs, 20, 0);
    lv_obj_align_to(box->page_device.btn_low_rs, box->page_device.mpump_info_box, LV_ALIGN_TOP_LEFT, 10, -15);
    lv_obj_set_style_bg_color(box->page_device.btn_low_rs, lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_size(box->page_device.btn_low_rs, 120, 40);
    lv_obj_add_event_cb(box->page_device.btn_low_rs, btn_low_rs_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *label_low_rs = lv_label_create(box->page_device.btn_low_rs);
    lv_obj_set_style_text_font(label_low_rs, device.ui.fnt.size_20.font, 0);
    lv_label_set_text(label_low_rs, "低转速");
    lv_obj_align_to(label_low_rs, box->page_device.btn_low_rs, LV_ALIGN_CENTER, 0, 0);

    box->page_device.btn_high_rs = lv_btn_create(box->page_device.mpump_info_box);
    lv_obj_set_style_radius(box->page_device.btn_high_rs, 20, 0);
    lv_obj_align_to(box->page_device.btn_high_rs, box->page_device.btn_low_rs, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_set_style_bg_color(box->page_device.btn_high_rs, lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_size(box->page_device.btn_high_rs, 120, 40);
    lv_obj_add_event_cb(box->page_device.btn_high_rs, btn_high_rs_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *label_hign_rs = lv_label_create(box->page_device.btn_high_rs);
    lv_obj_set_style_text_font(label_hign_rs, device.ui.fnt.size_20.font, 0);
    lv_label_set_text(label_hign_rs, "高转速");
    lv_obj_align_to(label_hign_rs, box->page_device.btn_high_rs, LV_ALIGN_CENTER, 0, 0);

    box->page_device.btn_mpump_reset = lv_btn_create(box->page_device.mpump_info_box);
    lv_obj_set_style_radius(box->page_device.btn_mpump_reset, 25, 0);
    lv_obj_align_to(box->page_device.btn_mpump_reset, box->page_device.btn_high_rs, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_set_style_bg_color(box->page_device.btn_mpump_reset, lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_size(box->page_device.btn_mpump_reset, 120, 50);
    box->page_device.label_mpump_reset = lv_label_create(box->page_device.btn_mpump_reset);
    lv_obj_set_style_text_font(box->page_device.label_mpump_reset, device.ui.fnt.size_20.font, 0);
    lv_label_set_text(box->page_device.label_mpump_reset, "故障复位");
    lv_obj_align_to(box->page_device.label_mpump_reset, box->page_device.btn_mpump_reset, LV_ALIGN_CENTER, 0, 0);
    // lv_obj_add_event_cb(box->page_device.btn_mpump_ctl, btn_high_rs_event_cb, LV_EVENT_CLICKED, NULL);

    box->page_device.mpump_status_box = lv_obj_create(box->page_device.mpump_box);
    lv_obj_set_style_bg_color(box->page_device.mpump_status_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.mpump_status_box, 0, 0);
    lv_obj_set_size(box->page_device.mpump_status_box, 350, 160);
    lv_obj_align_to(box->page_device.mpump_status_box, box->page_device.mpump_box, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_clear_flag(box->page_device.mpump_status_box, LV_OBJ_FLAG_SCROLLABLE);
    /*--------------------------------------------------------------------------------------*/
    box->page_device.mpump_status.timeout_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_TIMEOUT], "超时", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.overheat_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_OVERHEAT], "过热", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.overcurrent_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_OVERCURRENT], "过流", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.working_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_WORKING], "工作", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.high_speed_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_HIGH_SPEED], "高转速", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.normal_speed_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_NORMAL_SPEED], "正常转速", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.overload_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_OVERLOAD], "过载", 20, box->page_device.mpump_status_box, NULL);
    box->page_device.mpump_status.fail_start_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_MPM_FAIL_START], "启动失败", 20, box->page_device.mpump_status_box, NULL);
    lv_obj_align_to(box->page_device.mpump_status.timeout_box, box->page_device.mpump_status_box, LV_ALIGN_TOP_LEFT, -12, -10);
    lv_obj_align_to(box->page_device.mpump_status.overheat_box, box->page_device.mpump_status.timeout_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_obj_align_to(box->page_device.mpump_status.overcurrent_box, box->page_device.mpump_status.overheat_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_obj_align_to(box->page_device.mpump_status.working_box, box->page_device.mpump_status.overcurrent_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_obj_align_to(box->page_device.mpump_status.high_speed_box, box->page_device.mpump_status.timeout_box, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(box->page_device.mpump_status.normal_speed_box, box->page_device.mpump_status.high_speed_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_obj_align_to(box->page_device.mpump_status.overload_box, box->page_device.mpump_status.normal_speed_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_obj_align_to(box->page_device.mpump_status.fail_start_box, box->page_device.mpump_status.overload_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    /*--------------------------------------------------------------------------------------*/
    /*mpump box end*/

    /*jpump box start*/
    box->page_device.jpump_box = lv_obj_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.jpump_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.jpump_box, 0, 0);
    lv_obj_set_size(box->page_device.jpump_box, 200, 400);
    lv_obj_clear_flag(box->page_device.jpump_box, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_align_to(box->page_device.jpump_box, box->page_device.mpump_box, LV_ALIGN_OUT_RIGHT_BOTTOM, 5, 0);
    box->page_device.jpump_title = lv_label_create(box->box);
    lv_label_set_text(box->page_device.jpump_title, "机械泵");
    lv_obj_set_style_text_font(box->page_device.jpump_title, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.jpump_title, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_pad_all(box->page_device.jpump_title, 0, 0);
    lv_obj_align_to(box->page_device.jpump_title, box->page_device.jpump_box, LV_ALIGN_TOP_MID, 0, -5);
    box->page_device.jpump_info_box = lv_obj_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.jpump_info_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.jpump_info_box, 0, 0);
    lv_obj_set_size(box->page_device.jpump_info_box, 180, 260);
    lv_obj_align_to(box->page_device.jpump_info_box, box->page_device.jpump_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 3);
    lv_obj_clear_flag(box->page_device.jpump_info_box, LV_OBJ_FLAG_SCROLLABLE);

    box->page_device.jpump_voltage = draw_info_label_format(&lobj[LID_DEV_JPM_VOLTAGE], "电压:", box->page_device.jpump_info_box);
    box->page_device.jpump_current = draw_info_label_format(&lobj[LID_DEV_JPM_CURRENT], "电流:", box->page_device.jpump_info_box);
    box->page_device.jpump_rotational_speed = draw_info_label_format(&lobj[LID_DEV_JPM_RS], "转速:", box->page_device.jpump_info_box);
    lv_obj_set_style_text_font(lobj[LID_DEV_JPM_RS], device.ui.fnt.size_20.font, 0);
    box->page_device.jpump_temperature1 = draw_info_label_format(&lobj[LID_DEV_JPM_TEMPERATURE1], "温度1:", box->page_device.jpump_info_box);
    box->page_device.jpump_temperature2 = draw_info_label_format(&lobj[LID_DEV_JPM_TEMPERATURE2], "温度2:", box->page_device.jpump_info_box);
    lv_obj_align_to(box->page_device.jpump_voltage, box->page_device.jpump_info_box, LV_ALIGN_TOP_LEFT, -10, -5);
    lv_obj_align_to(lobj[LID_DEV_JPM_VOLTAGE], box->page_device.jpump_voltage, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(box->page_device.jpump_current, box->page_device.jpump_voltage, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_JPM_CURRENT], box->page_device.jpump_current, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(box->page_device.jpump_rotational_speed, box->page_device.jpump_current, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_JPM_RS], box->page_device.jpump_rotational_speed, LV_ALIGN_OUT_RIGHT_TOP, 10, 2);
    lv_obj_align_to(box->page_device.jpump_temperature1, box->page_device.jpump_rotational_speed, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_JPM_TEMPERATURE1], box->page_device.jpump_temperature1, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(box->page_device.jpump_temperature2, box->page_device.jpump_temperature1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(lobj[LID_DEV_JPM_TEMPERATURE2], box->page_device.jpump_temperature2, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);

    box->page_device.btn_jpump_reset = lv_btn_create(box->page_device.jpump_info_box);
    lv_obj_set_style_radius(box->page_device.btn_jpump_reset, 40, 0);
    lv_obj_align_to(box->page_device.btn_jpump_reset, box->page_device.jpump_temperature2, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 10);
    lv_obj_set_style_bg_color(box->page_device.btn_jpump_reset, lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_size(box->page_device.btn_jpump_reset, 120, 50);
    box->page_device.label_jpump_reset = lv_label_create(box->page_device.btn_jpump_reset);
    lv_obj_set_style_text_font(box->page_device.label_jpump_reset, device.ui.fnt.size_20.font, 0);
    lv_label_set_text(box->page_device.label_jpump_reset, "故障复位");
    lv_obj_align_to(box->page_device.label_jpump_reset, box->page_device.btn_jpump_reset, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(box->page_device.label_jpump_reset, btn_jpump_reset_event_cb, LV_EVENT_CLICKED, NULL);

    box->page_device.jpump_status_box = lv_obj_create(box->page_device.jpump_box);
    lv_obj_set_style_bg_color(box->page_device.jpump_status_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.jpump_status_box, 0, 0);
    lv_obj_set_size(box->page_device.jpump_status_box, 180, 80);
    lv_obj_align_to(box->page_device.jpump_status_box, box->page_device.jpump_info_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_obj_clear_flag(box->page_device.jpump_status_box, LV_OBJ_FLAG_SCROLLABLE);
    /*--------------------------------------------------------------------------------------*/
    box->page_device.jpump_status.working_box = draw_btn_radio_format(&lobj[LID_DEV_RADIO_JPM_WORKING], "工作", 20, box->page_device.jpump_status_box, NULL);
    box->page_device.jpump_status.fault = draw_btn_radio_format(&lobj[LID_DEV_RADIO_JPM_FAULT], "故障", 20, box->page_device.jpump_status_box, NULL);
    lv_obj_align_to(box->page_device.jpump_status.working_box, box->page_device.jpump_status_box, LV_ALIGN_TOP_LEFT, -12, -15);
    lv_obj_align_to(box->page_device.jpump_status.fault, box->page_device.jpump_status.working_box, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    /*--------------------------------------------------------------------------------------*/
    /*jpump box end*/

    /*lock&unlock box start*/
    box->page_device.btn_unlock = lv_btn_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.btn_unlock, lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_style_radius(box->page_device.btn_unlock, 0, 0);
    lv_obj_set_size(box->page_device.btn_unlock, 200, 50);
    lv_obj_align_to(box->page_device.btn_unlock, box->page_device.device_ctl_box, LV_ALIGN_OUT_TOP_LEFT, 0, -5);
    lv_obj_add_event_cb(box->page_device.btn_unlock, btn_unlock_cb, LV_EVENT_CLICKED, NULL);

    box->page_device.label_unlock = lv_label_create(box->page_device.btn_unlock);
    lv_label_set_text(box->page_device.label_unlock, "开锁");
    lv_obj_set_style_text_font(box->page_device.label_unlock, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.label_unlock, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(box->page_device.label_unlock, box->page_device.btn_unlock, LV_ALIGN_CENTER, 0, 0);

    box->page_device.btn_lock = lv_btn_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.btn_lock, lv_color_hex(0x2F2F3F), 0);
    lv_obj_set_style_radius(box->page_device.btn_lock, 0, 0);
    lv_obj_set_size(box->page_device.btn_lock, 200, 50);
    lv_obj_align_to(box->page_device.btn_lock, box->page_device.btn_unlock, LV_ALIGN_OUT_TOP_LEFT, 0, -5);
    lv_obj_add_event_cb(box->page_device.btn_lock, btn_lock_cb, LV_EVENT_CLICKED, NULL);

    box->page_device.label_lock = lv_label_create(box->page_device.btn_lock);
    lv_label_set_text(box->page_device.label_lock, "关锁");
    lv_obj_set_style_text_font(box->page_device.label_lock, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.label_lock, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(box->page_device.label_lock, box->page_device.btn_lock, LV_ALIGN_CENTER, 0, 0);
    /*lock&unlock box end*/
    /*vacuum box start*/
    box->page_device.vacuum_box = lv_obj_create(box->box);
    lv_obj_set_style_bg_color(box->page_device.vacuum_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.vacuum_box, 0, 0);
    lv_obj_set_size(box->page_device.vacuum_box, 575, 105);
    lv_obj_align_to(box->page_device.vacuum_box, box->page_device.mpump_box, LV_ALIGN_OUT_TOP_LEFT, 0, -5);
    lv_obj_clear_flag(box->page_device.vacuum_box, LV_OBJ_FLAG_SCROLLABLE);

    box->page_device.vacuum_title = lv_label_create(box->page_device.vacuum_box);
    lv_label_set_text(box->page_device.vacuum_title, "真空计");
    lv_obj_set_style_text_font(box->page_device.vacuum_title, device.ui.fnt.size_24.font, 0);
    lv_obj_set_style_text_align(box->page_device.vacuum_title, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(box->page_device.vacuum_title, box->page_device.vacuum_box, LV_ALIGN_TOP_MID, 0, -5);

    box->page_device.vacuum_info_box = lv_obj_create(box->page_device.vacuum_box);
    lv_obj_set_style_bg_color(box->page_device.vacuum_info_box, lv_color_hex(0x1F1F2F), 0);
    lv_obj_set_style_radius(box->page_device.vacuum_info_box, 0, 0);
    lv_obj_set_size(box->page_device.vacuum_info_box, 500, 50);
    lv_obj_align_to(box->page_device.vacuum_info_box, box->page_device.vacuum_box, LV_ALIGN_BOTTOM_LEFT, 15, 12);
    lv_obj_clear_flag(box->page_device.vacuum_info_box, LV_OBJ_FLAG_SCROLLABLE);

    box->page_device.vacuum_label = lv_label_create(box->page_device.vacuum_info_box);
    lv_obj_set_style_text_font(box->page_device.vacuum_label, device.ui.fnt.size_24.font, 0);
    lv_label_set_text(box->page_device.vacuum_label, "真空值:");
    lv_obj_set_style_text_align(box->page_device.vacuum_label, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(box->page_device.vacuum_label, box->page_device.vacuum_info_box, LV_ALIGN_LEFT_MID, 10, 0);

    lobj[LID_DEV_VACUUM_LABEL] = lv_label_create(box->page_device.vacuum_info_box);
    lv_obj_set_style_text_font(lobj[LID_DEV_VACUUM_LABEL], device.ui.fnt.size_24.font, 0);
    lv_label_set_text(lobj[LID_DEV_VACUUM_LABEL], "0.0 Pa");
    lv_obj_set_style_text_align(lobj[LID_DEV_VACUUM_LABEL], LV_ALIGN_CENTER, 0);
    lv_obj_align_to(lobj[LID_DEV_VACUUM_LABEL], box->page_device.vacuum_label, LV_ALIGN_OUT_RIGHT_TOP, 20, 0);

    box->page_device.vacuum_cold_gauge_label = lv_label_create(box->page_device.vacuum_info_box);
    lv_obj_set_style_text_font(box->page_device.vacuum_cold_gauge_label, device.ui.fnt.size_24.font, 0);
    lv_label_set_text(box->page_device.vacuum_cold_gauge_label, "冷规状态:");
    lv_obj_set_style_text_align(box->page_device.vacuum_cold_gauge_label, LV_ALIGN_CENTER, 0);
    lv_obj_align_to(box->page_device.vacuum_cold_gauge_label, lobj[LID_DEV_VACUUM_LABEL], LV_ALIGN_OUT_RIGHT_TOP, 90, 0);

    lobj[LID_DEV_VACUUM_COLD_GAUGE_LABEL] = lv_label_create(box->page_device.vacuum_info_box);
    lv_obj_set_style_text_font(lobj[LID_DEV_VACUUM_COLD_GAUGE_LABEL], device.ui.fnt.size_24.font, 0);
    lv_label_set_text(lobj[LID_DEV_VACUUM_COLD_GAUGE_LABEL], "空闲");
    lv_obj_set_style_text_align(lobj[LID_DEV_VACUUM_COLD_GAUGE_LABEL], LV_ALIGN_CENTER, 0);
    lv_obj_align_to(lobj[LID_DEV_VACUUM_COLD_GAUGE_LABEL], box->page_device.vacuum_cold_gauge_label, LV_ALIGN_OUT_RIGHT_TOP, 20, 0);

    /*vacuum box end*/
}
void jpump_ctl_cb(lv_event_t *event)
{
    printf("jpump_ctl_cb\n");
    uint16_t jpump_status;
    uint16_t jpump_start = 0x0002;
    uint16_t jpump_close = 0x0005;

    slave485_err_t slave485_err = slave485_read_reg(&device.slave485, 0x0061, &jpump_status, 1);
    printf("jpump_status:%04x\n", jpump_status);
    if (jpump_status == 0x0002)
        slave485_err = slave485_write_reg(&device.slave485, 0x0060, jpump_close);
    else if (jpump_status == 0x0005)
        slave485_err = slave485_write_reg(&device.slave485, 0x0060, jpump_start);
    if (slave485_err != SLAVE485_OK)
        printf("slave485_err:%d\n", slave485_err);
}

void valve_main_ctl_cb(lv_event_t *event)
{
    uint16_t valve_main_status;
    slave485_read_coil(&device.slave485, 0x0002, &valve_main_status, 1);
    if (valve_main_status == 0x0000)
    {
        slave485_write_coil(&device.slave485, 0x0002, 0xFF00);
    }
    else
    {
        slave485_write_coil(&device.slave485, 0x0002, 0x0000);
    }
}

void mpump_switch_cb(lv_event_t *event)
{
    printf("mpump switch cb.\n");
    uint8_t mpump_status;
    // uint16_t mpump_r_speed;
    uint16_t vacuum_value[2];
    uint16_t mpump_start = 0x0008;
    uint16_t mpump_close = 0x0003;
    slave485_read_reg(&device.slave485, VACUUM_FIRST_ADR, vacuum_value, 2);
    double vacuum_value_d = ((vacuum_value[0] >> 8) - '0');
    vacuum_value_d += (double)((vacuum_value[0] & 0xFF) - '0') * 0.1;
    int powx = (vacuum_value[1] & 0xFF) - '0';
    if ((vacuum_value[1] >> 8) == '-')
    {
        powx = -powx;
    }
    vacuum_value_d = vacuum_value_d * pow(10, powx);
    slave485_err_t slave485_err = slave485_read_coil(&device.slave485, 0x0010, &mpump_status, 8);
    // slave485_err = slave485_read_reg(&device.slave485, 0x0014, &mpump_r_speed, 1);
    printf("mpump status:[%02x]\n", mpump_status);
    int mpump_switch_status = 0;
    mpump_switch_status = (mpump_status >> 4) & 1;
    printf("mpump_switch_status value:[%d]\n", mpump_switch_status);
    if (mpump_switch_status == 0)
    {
        printf("vacuum_value_d value:[%f]\n", vacuum_value_d);
        if (vacuum_value_d < 50)
        {
            slave485_err = slave485_write_reg(&device.slave485, 0x0010, 0x0008);
            printf("mpump write failed:[%d]\n", slave485_err);
        }
        else
            show_toast_o("等待低真空...", 500);
    }
    else
        slave485_write_reg(&device.slave485, 0x0010, 0x0003);

    if (slave485_err != SLAVE485_OK)
        printf("slave485_err:%d\n", slave485_err);
}

void device_ctl_cb(lv_event_t *event)
{
    if (device.autorun.start)
    {
        device.autorun.start = false;
    }
    else
    {
        device.autorun.start = true;
    }
}
void btn_jpump_reset_event_cb(lv_event_t *event)
{
    uint16_t jpump_status;
    slave485_read_reg(&device.slave485, 0x0061, &jpump_status, 1);

    if (jpump_status == 0x0006)
    {

        slave485_write_reg(&device.slave485, 0x0060, 0x0007);
        show_toast_o("故障复位成功", 200);
    }
    else
        show_toast_o("未发现故障", 200);
}

void btn_unlock_cb(lv_event_t *event)
{
    show_lock_box(true, true, true, true, unlock_cb, NULL);
}
void unlock_cb(void *args)
{
}
void btn_lock_cb(lv_event_t *event)
{
    // md_reg_sync_list_add(&device.modbus, MDCMD_WRITE_REG, ESP32_ADDR, ESP32_REG_ADR_DEVICE_STATUS, 0, device.esp32.device_status_b &= ~0x0001, 300, NULL);
}

void btn_high_rs_event_cb(lv_event_t *event)
{
    uint16_t mpump_status;
    slave485_err_t slave485_err = slave485_read_coil(&device.slave485, 0x0010, &mpump_status, 8);
    int mpump_switch_status = 0;
    mpump_switch_status = (mpump_status >> 4) & 1;
    if (mpump_switch_status == 1)
    {
        slave485_err = slave485_write_reg(&device.slave485, 0x0010, 0x0001);
    }
    else
    {
        show_toast_o("分子泵未启动", 200);
    }
}
void btn_low_rs_event_cb(lv_event_t *event)
{
    uint16_t mpump_status;
    slave485_err_t slave485_err = slave485_read_coil(&device.slave485, 0x0010, &mpump_status, 8);
    int mpump_switch_status = 0;
    mpump_switch_status = (mpump_status >> 4) & 1;
    if (mpump_switch_status == 1)
    {
        slave485_err = slave485_write_reg(&device.slave485, 0x0010, 0x0002);
    }
    else
    {
        show_toast_o("分子泵未启动", 200);
    }
}

void hook_match(string hook, lv_obj_t *obj)
{
    if (strcmp(hook, "LID_MP_GLOMMY") == 0)
    {
        // printf("hook match: %s\n", hook);
        lobj[LID_PM_GLOMMY] = obj;
    }
}

/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
